package com.hantanger.weblog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Text;
import com.hantanger.weblog.model.Category;
import com.hantanger.weblog.model.Post;
import com.hantanger.weblog.util.HtmlUtil;
import com.hantanger.weblog.util.QueueUtil;
import com.hantanger.weblog.util.TagUtil;
import com.hantanger.weblog.util.UserHelper;

/**
 * 日志编辑与日志发表
 * @author lys
 *
 */
public class PostEditController extends BaseController {

	private String noUserAdmin(HttpServletRequest request){
		if(!UserHelper.isAdmin()){
			request.setAttribute("errorMsg", "无此操作权限！");
			return "error";
		}
		return null;
	}
	/**
	 * 进入编辑页面
	 * @param request
	 * @param response
	 * @return
	 */
	public String doGet(HttpServletRequest request,
			HttpServletResponse response) {
		if(noUserAdmin(request) != null){
			return "error";
		}
		Long postId = HtmlUtil.getLong(request, "postId", 0L);
		if(postId > 0){
			Post post = getPostService().getPostById(postId, false);
			request.setAttribute("post", post);
		}
		return "postedit";
	}
	
	/**
	 * 发布日志逻辑
	 * @param modelAndView
	 * @param request
	 * @param response
	 * @return
	 */
	public String doPost(HttpServletRequest request,
			HttpServletResponse response) {
		if(noUserAdmin(request) != null){
			return "error";
		}
		Long postId = HtmlUtil.getLong(request, "postId", 0L);
		String title = HtmlUtil.getString(request, "title", "");
		String content = HtmlUtil.getString(request, "content", "");
		String tags = HtmlUtil.getString(request, "tags", "");
		String keyword = HtmlUtil.getString(request, "keyword", "");
		String digest = HtmlUtil.getString(request, "digest", "");
		Long categoryId = HtmlUtil.getLong(request, "category", -1);
		boolean publiced = HtmlUtil.getBool(request, "publiced", true);
		boolean allowComment = HtmlUtil.getBool(request, "allowComment", true);
		boolean published = HtmlUtil.getBool(request, "p", true);

		boolean isNew = true;
		Post post = null;
		if (postId > 0) {
			isNew = false;
			post = getPostService().getPostById(postId, false);
		} else {
			post = new Post();
		}
		post.setTitle(HtmlUtil.repairHtml(title));
		post.setContent(new Text(HtmlUtil.repairHtml(content)));
		post.setDigest(new Text(HtmlUtil.repairHtml(digest)));
		post.setPubliced(publiced);
		post.setCategoryId(categoryId);
		post.setAllowComment(allowComment);
		post.setPublished(published);
		
		post.setTags(TagUtil.processTag(tags));
		post.setKeyword(keyword);

		if(isNew){
			post.setAuthorId(UserHelper.getCurrentUser().getId());
			getPostService().insert(post);
		}else{
			getPostService().update(post);
		}
		
		if(post.isPubliced() && post.isPublished() && UserHelper.isOnlineEnv()){
			Map<String, String> params = new HashMap<String, String>();
			String nickname = getSettingDao().getDefaultSetting().getSpaceName();
			if(nickname == null){
				nickname = "";
			}
			params.put("nickname", nickname);
			params.put("blogUrl", getSpaceUrl());
			params.put("postUrl", getSpaceUrl() + "/post/prev/" + post.getId());
			params.put("rssUrl", getSpaceUrl() + "/rss");
			QueueUtil.add("/task/blogping", params);
		}
		Map<String,Long> data = new HashMap<String,Long>();
		data.put("postId", post.getId());
		renderJson(data, response, JSON);
		return null;
	}
	
	@Override
	protected void fillSideBarData(HttpServletRequest request){
		List<Category> categorys = getPostService().getAllCategorys();
		request.setAttribute("categorys", categorys);
	}
}
